from util.selenium.webdriver.common.by import By

from base.BaseMethod import BM
from util.DataQuery.FormDataDecorator import FormDataDecorator
from util.g import g


class WriteRdcostForm():
    form_btn_loc = (By.LINK_TEXT, '企业年度研究开发费用结构明细表')
    content_iframe_loc = (By.CSS_SELECTOR, "#innocomFrame")
    #    添加按钮
    # add_btn_loc = (By.CSS_SELECTOR, '.form-group.pull-right .blue')
    add_btn_loc = (By.XPATH, "//div[@class='form-group pull-right']/button[contains(string(), '添加')]")
    #    删除按钮
    # delete_btn_loc = (By.CSS_SELECTOR, '.table-search div:nth-of-type(2) button')
    delete_btn_loc = (By.XPATH, "//div[@class='form-group pull-right']/button[contains(string(), '删除')]")
    # 项目编号。
    pro_num_loc = (By.CSS_SELECTOR, 'select#xmbh')
    # # # 共计 ----------- 系统自动计算字段
    total_loc = (By.CSS_SELECTOR, 'input#nbyjkftr')
    # 人员人工费用
    ryrg_loc = (By.CSS_SELECTOR, 'input#ryrg')
    # 直接投入费用
    zjtr_loc = (By.CSS_SELECTOR, 'input#zjtr')
    # 折旧费用
    zjfy_loc = (By.CSS_SELECTOR, 'input#zjfy')
    # 无形资产
    wxzc_loc = (By.CSS_SELECTOR, 'input#wxzc')
    # 设计费
    sjf_loc = (By.CSS_SELECTOR, 'input#sjf')
    # 装备调试
    sbtsf_loc = (By.CSS_SELECTOR, 'input#sbtsf')
    # 其他费用
    qtfy_loc = (By.CSS_SELECTOR, 'input#qtfy')
    # 委托外部研究开发费用
    wtwbyjkftr_loc = (By.CSS_SELECTOR, 'input#wtwbyjkftr')
    # 境内的外部研发费用
    pjnwbtrhj_loc = (By.CSS_SELECTOR, 'input#pjnwbtrhj')
    # # # # # # #  研究开发费用 --------- 共计 ------- 系统自动计
    nwhj_loc = (By.CSS_SELECTOR, 'input#nwhj')
    # 企业填报人
    fill_user_loc = (By.CSS_SELECTOR, 'input#fillUser')
    # 企业填报人签字日期
    fill_date_loc = (By.CSS_SELECTOR, 'input#fillUserDate')
    fill_date_js_str = "document.getElementById('fillUserDate').removeAttribute('readonly')"
    # 排序号
    order_num_loc = (By.CSS_SELECTOR, 'input#orderNo')

    save_btn_loc = (By.CSS_SELECTOR, '.box-footer.text-right button#dataEprRdfeesave')
    back_btn_loc = (By.CSS_SELECTOR, '.box-footer.text-right button.btn-default')

    next_page_loc = (By.CSS_SELECTOR, ".pagination.pull-right li:nth-of-type(4) a")

    page_num_loc = (By.CSS_SELECTOR, ".pagination.pull-right li:nth-of-type(3) a")
    select_all_loc = (By.CSS_SELECTOR, "input#items_selector")

    delete_1 = False
    delete_2 = False
    delete_3 = False
    # 写代码时方便提示, 基本方法完成后删除
    # driver = driver
    def __init__(self, driver=None):
        self.driver = driver
        self.record_var = "{}Record".format(self.class_name)
        setattr(g, self.record_var, list())

    @property
    def class_name(self):
        return self.__class__.__name__

    def input_cost(self, int_amount='', labor='', direct_inv='', de_lta='', aia='', design='', ecct='', other='', ext_amount='', ext_inchina=''):
        # 系统自动计算， 不需要填写。
        # BM.input(ele_loc=self.total_loc, driver=self.driver, keys=int_amount)
        BM.input(ele_loc=self.ryrg_loc, driver=self.driver, keys=labor)
        BM.input(ele_loc=self.zjtr_loc, driver=self.driver, keys=direct_inv)
        BM.input(ele_loc=self.zjfy_loc, driver=self.driver, keys=de_lta)
        BM.input(ele_loc=self.wxzc_loc, driver=self.driver, keys=aia)
        BM.input(ele_loc=self.sjf_loc, driver=self.driver, keys=design)
        BM.input(ele_loc=self.sbtsf_loc, driver=self.driver, keys=ecct)
        BM.input(ele_loc=self.qtfy_loc, driver=self.driver, keys=other)
        BM.input(ele_loc=self.wtwbyjkftr_loc, driver=self.driver, keys=ext_amount)
        BM.input(ele_loc=self.pjnwbtrhj_loc, driver=self.driver, keys=ext_inchina)

    def input_fill_user_info(self, amount='', reporter='', sig_date='', sort_num=''):
        BM.input(ele_loc=self.fill_user_loc, driver=self.driver, keys=reporter)
        self.driver.execute_script(self.fill_date_js_str)
        BM.input(ele_loc=self.fill_date_loc, driver=self.driver, keys=sig_date)
        BM.input(ele_loc=self.order_num_loc, driver=self.driver, keys=sort_num)
    def click_save_btn(self):
        return BM.global_click_btn(btn_loc=self.save_btn_loc, driver=self.driver)

    @FormDataDecorator
    def main(self, rd_id='', peryear='', int_amount='', int_labor='', int_direct_inv='', int_de_lta='', int_aia='', int_design='', int_ecct='', int_other='',ext_amount='', ext_inchina='',amount='', reporter='', sig_date='', sort_num=''):

        # # 这里 int_amount, amount 两个参数都不需要填写，系统会自动生成

        year_diff_num = g.year - peryear

        peryear_loc = (By.CSS_SELECTOR, '#attendance li:nth-of-type(%s) a' % str(year_diff_num))
        # BM.click(driver=self.driver, ele_loc=self.form_btn_loc)
        # BM.click(driver=self.driver, ele_loc=peryear_loc)

        # delete_variable = "delete_%s" % str(year_diff_num)
        # if g.is_first_entry and getattr(self, delete_variable):
        #     BM.click(driver=self.driver, ele_loc=self.form_btn_loc)
        #     BM.click(driver=self.driver, ele_loc=peryear_loc)
        #     BM.delete_all(driver=self.driver, page_num_loc=self.page_num_loc, select_all_loc=self.select_all_loc, delete_btn_loc=self.delete_btn_loc)
        #     setattr(self, delete_variable, True)
        is_click = False
        class_name = self.class_name
        li_var = "{}_{}".format(class_name, year_diff_num)
        if not hasattr(g, li_var):
            setattr(g, li_var, list())
        if getattr(g, li_var) == []:
            is_click = True
            try:
                BM.click(driver=self.driver, ele_loc=self.form_btn_loc, wait="driver_wait")
            except Exception as err:
                self.driver.switch_to.parent_frame()
                BM.click(driver=self.driver, ele_loc=self.form_btn_loc)
            BM.switch_to_frame(driver=self.driver, ele_loc=self.content_iframe_loc)
            BM.click(driver=self.driver, ele_loc=peryear_loc)
            if g.is_first_entry:
                # is_click = True
                # BM.click(driver=self.driver, ele_loc=self.form_btn_loc)
                # BM.switch_to_frame(driver=self.driver, ele_loc=self.content_iframe_loc)
                BM.delete_all(driver=self.driver, page_num_loc=self.page_num_loc, select_all_loc=self.select_all_loc, delete_btn_loc=self.delete_btn_loc)
                # self.delete = True

            a_list = getattr(g, li_var)
            table_loc = (By.CSS_SELECTOR, ".tableBody")
            tr_loc = (By.CSS_SELECTOR, ".tableBody tr td:nth-of-type(2)")
            while True and g.repetition:
                BM.wait(driver=self.driver, ele_loc=table_loc)
                data = self.driver.find_elements(*tr_loc)
                for i in data:
                    # 获取 标签中的 汉字
                    sta_name_str = i.text
                    a_list.append(sta_name_str)

                next_btn = BM.get_ele(driver=self.driver, ele_loc=self.next_page_loc)
                onclick = next_btn.get_attribute("onclick")
                if not onclick:
                    # BM.click(driver=self.driver, ele_loc=self.next_page_loc)
                    break
                else:
                    next_btn.click()

        li_list = getattr(g, li_var)
        class_record = getattr(g, self.record_var)
        pro_num = 'RD' + str(rd_id).zfill(2) if rd_id != 0 else "其他"
        if (year_diff_num > 3):
            class_record.append(pro_num)
            return True
        if li_list != []:
            for li in li_list:
                if li not in class_record:
                    class_record.append(li)
        if pro_num in li_list:
            return
        g.logger.info("{}： {}".format(class_name, class_record))

        # if not is_click:
        #     BM.click(driver=self.driver, ele_loc=self.form_btn_loc)
        BM.click(driver=self.driver, ele_loc=peryear_loc)

        BM.click(driver=self.driver, ele_loc=self.add_btn_loc)
        # TODO 这里选择项目编号使用 select 方法

        BM.select_value_choice(driver=self.driver, selector_loc=self.pro_num_loc, text_value=pro_num)

        BM.wait(driver=self.driver, ele_loc=self.save_btn_loc)
        # # # # 数据显示为 RD01、 RD02 ~
        # select_value_choice(selector_loc=self.pro_num_loc, text_value=rd_id, driver=self.driver)

        self.input_cost(labor=int_labor, direct_inv=int_direct_inv, de_lta=int_de_lta, aia=int_aia, design=int_design, ecct=int_ecct, other=int_other, ext_amount=ext_amount, ext_inchina=ext_inchina)
        self.input_fill_user_info(reporter=reporter, sig_date=sig_date, sort_num=sort_num)

        res = self.click_save_btn()

        if res:
            class_record.append(pro_num)

        # click_back_btn(btn_loc=self.back_btn_loc, driver=self.driver)
